package com.autorun.server.controller;

import com.autorun.server.dto.ApiResponse;
import com.autorun.server.entity.Apk;
import com.autorun.server.service.ApkService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 公共APK控制器（无需权限验证）
 * 
 * @author autorun
 * @version 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/api/apk")
public class PublicApkController {

    @Autowired
    private ApkService apkService;

    /**
     * 获取最新APK
     * 
     * @param game 游戏名称
     * @return APK信息
     */
    @GetMapping("/latest")
    public ApiResponse<Apk> getLatestApk(@RequestParam(required = false) String game) {
        try {
            Apk apk = apkService.getLatestApk(game);
            return ApiResponse.success(apk);
        } catch (Exception e) {
            log.error("获取最新APK失败: {}", e.getMessage(), e);
            return ApiResponse.error("获取最新APK失败: " + e.getMessage());
        }
    }

    /**
     * 获取APK下载地址
     * 
     * @param id APK ID
     * @return 下载地址
     */
    @GetMapping("/{id}/download")
    public ApiResponse<String> getDownloadUrl(@PathVariable Integer id) {
        try {
            String downloadUrl = apkService.getDownloadUrl(id);
            return ApiResponse.success(downloadUrl);
        } catch (Exception e) {
            log.error("获取下载地址失败: {}", e.getMessage(), e);
            return ApiResponse.error("获取下载地址失败: " + e.getMessage());
        }
    }
}